package com.controller;

import com.entity.Td_tbreadlog;
import com.service.inte.StudyServiceInte;
import com.service.inte.TeamServiceInte;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class StudyController {

    @Autowired
    private StudyServiceInte studyServiceInte;
    @Autowired
    private TeamServiceInte teamServiceInte;

    @GetMapping("turnCateGory")
    public String turnCateGory(){
        return "study/category.html";
    }

    //分类型查询
    @PostMapping("getCategory")
    @ResponseBody
    public List<Object[]> getCategory(int typeId,int page){
        System.out.println("学习详情");
        return studyServiceInte.getTeamByTypeDescCreateTime(1866,typeId,page);
    }

    //最新三条新闻
    @GetMapping("getThreeNew")
    @ResponseBody
    public List<Object[]> getThreeNew(){
        return studyServiceInte.getThreeNew();
    }

    //父级节点为18的轮播新闻
    @GetMapping("getLunbo")
    @ResponseBody
    public List<Object[]> getLunbo(){
        return teamServiceInte.getRollByFatherType(18);
    }

    //阅读数最多的三条新闻
    @GetMapping("getThreeMaxTotal")
    @ResponseBody
    public List<Object[]> getThreeMaxTotal(){
        return studyServiceInte.getThreeTeamByMaxTotal();
    }

    @GetMapping("turntoStudy")
    public String turntoStudy(){
        return "study/index.html";
    }

    //跳转到详情页
    @GetMapping("getStudyDetail")
    public String getDetail(int id, HttpServletRequest request){
        request.setAttribute("detail",teamServiceInte.getDetail(id));
        Td_tbreadlog tbreadlog =new Td_tbreadlog();
        if (studyServiceInte.getByTeamId8LoginId(id,1866)!=null){
            tbreadlog=studyServiceInte.getByTeamId8LoginId(id,1866);
        }else{
            tbreadlog.setFd_loginid(1866);
            tbreadlog.setFd_readtotal(0);
            tbreadlog.setFd_teamid(id);
        }


        request.setAttribute("readlog",tbreadlog);
        return "study/detail.html";
    }

    @GetMapping("getTeamByMaxTotal")
    @ResponseBody
    public List<Object[]> getTeamByMaxTotal(int page){
        return studyServiceInte.getTeamByMaxTotal(1866,page);
    }

    @GetMapping("turnRecommend")
    public String turnRecommend(){
        return "study/recommend.html";
    }

    @GetMapping("saveReadLog")
    public String saveReadLog(int teamId){
        Td_tbreadlog readLog = studyServiceInte.getByTeamId8LoginId(teamId, 1866);
        if (readLog==null){
            readLog=new Td_tbreadlog();
            readLog.setFd_teamid(teamId);
            readLog.setFd_readtotal(0);
            readLog.setFd_loginid(1866);
        }else {
            readLog.setFd_readtotal(readLog.getFd_readtotal()+1);
        }
        System.out.println(readLog);
        studyServiceInte.saveReadLog(readLog);
        return "study/index.html";
    }
}

